package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 目标：
 *
 * @author Ashely
 * @date 2024/6/17 10:12
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        if (shoppingCartList!=null && shoppingCartList.size()==1){
            ShoppingCart sc = shoppingCartList.get(0);
            shoppingCart.setId(sc.getId());
            shoppingCart.setNumber(sc.getNumber()+1);
            shoppingCartMapper.update(shoppingCart);
        }else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            if (shoppingCartDTO.getDishId()!=null){
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                String dishName = dish.getName();
                String dishImage = dish.getImage();
                BigDecimal dishPrice = dish.getPrice();
                shoppingCart.setName(dishName);
                shoppingCart.setImage(dishImage);
                shoppingCart.setAmount(dishPrice);
            } else {
                Setmeal setmeal = setmealMapper.selectById(shoppingCartDTO.getSetmealId());
                String setmealName = setmeal.getName();
                String setmealImage = setmeal.getImage();
                BigDecimal setmealPrice = setmeal.getPrice();
                shoppingCart.setName(setmealName);
                shoppingCart.setImage(setmealImage);
                shoppingCart.setAmount(setmealPrice);
            }
            shoppingCartMapper.add(shoppingCart);
            
        }
        
      






    }

    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    @Override
    public void clean() {
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        if (shoppingCartList!=null && shoppingCartList.size()==1){
            ShoppingCart sc = shoppingCartList.get(0);
            shoppingCart.setId(sc.getId());
            if (sc.getNumber()==1){
                shoppingCartMapper.delete(sc.getId());
            }else {
                shoppingCart.setNumber(sc.getNumber()-1);
                shoppingCartMapper.update(shoppingCart);
            }
            
        }


    }
}
